package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.liondance.LionDanceConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.liondance.LionDanceMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class LionDanceActivityRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        LionDanceConfig config = LionDanceMgr.getConfig();
        if (config != null ) {
            List<String> paramList = new ArrayList<>();
            //参数1：待回收的道具
            paramList.add(String.valueOf(config.getConsumeItemId()));
            paramList.add(config.getActivityInfo().getActivityName());
            //回收时间是活动过期后+10分钟
            long recycleTime = config.getActivityInfo().getEndShowTime() * 1000L + 600 * 1000;
            ActivityItemRecycleMgr.addRecycleItemData(config.getConsumeItemId(), activityType, 1, recycleTime, paramList);
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.LionDance.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        if(data.getRecycleType() == 1){
            String language = UserMgr.getUserInfo(userId).getLanguage();
            UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                // 开宝箱
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                if(goodsInfo != null){
                    long num = userBagItem.getNum();
                    if (removeItem(userBagItem)){
                        Property reward = new Property();
                        for (long i = 0; i < num; i++) {
                            OpenGoodsBagResult result = GoodsBagMgr.getRandomGoods(data.getItemId());
                            reward.addProperty(result.getReward());
                        }
                        //  发邮件
                        String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                        String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
                        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_TITLE, language), activityName);
                        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_CONTENT,
                                language), removeName, num);
                        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
                    }
                }
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
